package com.qf.smsplatform.monitor.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author: LeeRui
 * 2021/2/3 下午 8:17
 **/
@Component
public class JavaMailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${mail.from}")
    private String from;

    @Value("${mail.to}")
    private String to;

    @Value("${mail.subject}")
    private String subject;

    //发送短信通知客户队列消息堆积过多
    @Async
    public void sendMailForQueue(String queueName,Integer messageCount) throws MessagingException {
        // 创建邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        // 封装发件人、收件人、主题、内容
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText("text/html;charset=utf-8","<h1 style='color:red;'>RabbitMQ中"+queueName+"的消息堆积了，目前消息共有"+messageCount+"条,请及时处理！！！</h1>");

        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

    //发送短信通知客户队列消息堆积过多
    @Async
    public void sendMailForFee(Long fee) throws MessagingException {
        // 创建邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        // 封装发件人、收件人、主题、内容
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText("text/html;charset=utf-8","<h1 style='color:red;'>您好！您的账户余额不足1000,当前余额为："+fee+"，请及时充值，以免影响您使用我们的短信服务，谢谢，祝您生活愉快！！！！</h1>");

        // 发送邮件
        javaMailSender.send(mimeMessage);
    }
}
